CREATE PROCEDURE [dbo].[UserSave]
@Id				UNIQUEIDENTIFIER, 
@UserName			NVARCHAR(20),
@Password		NVARCHAR(20),
@FirstName		NVARCHAR(20),
@LastName		NVARCHAR(20),
@Email			NVARCHAR(50),
@Role			INT,
@RefUserId		UNIQUEIDENTIFIER

AS

DECLARE	@RowCount AS INT
DECLARE @Error AS INT

UPDATE	[User]
SET		UserName		= ISNULL(@UserName,[UserName]),
		Password		= ISNULL(@Password,Password),
		FirstName		= ISNULL(@FirstName,FirstName),
		LastName		= ISNULL(@LastName, LastName),
		Email			= ISNULL(@Email, Email),
		Role			= ISNULL(@Role, Role),
		RefUserId		= ISNULL(@RefUserId, RefUserId)
WHERE	Id = @Id

SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNT

IF @Error  <> 0
BEGIN
	RAISERROR('ERROR::UserSave',16,1)
END

IF @RowCount  > 0
BEGIN
	RETURN @RowCount 
END

--	If adding for first time entering all the details
INSERT	INTO [User] (Id, UserName, Password, FirstName, LastName, Email, Role, RefUserId)
VALUES	(@Id, @UserName, @Password, @FirstName, @LastName, @Email, @Role, @RefUserId)

SELECT	@Error = @@ERROR, @RowCount = @@ROWCOUNT

IF @Error  <> 0
BEGIN
	RAISERROR('ERROR::UserNew',16,1)
END

RETURN @RowCount
   